package com.niit.onlinemusicserver.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.niit.onlinemusicserver.common.JwtUtils;
import com.niit.onlinemusicserver.common.R;
import com.niit.onlinemusicserver.common.Token;
import com.niit.onlinemusicserver.model.domain.Consumer;
import com.niit.onlinemusicserver.model.request.ConsumerRequest;
import com.niit.onlinemusicserver.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
public class ConsumerController {
  @Autowired
  private ConsumerService consumerService;
  /**
   *管理界面调用：返回所有用户
   */
  @GetMapping("/user")
  public R allUser(@RequestParam(defaultValue = "1") int curPage,@RequestParam(defaultValue = "1") int pageSize) {
    IPage<Consumer> page = new Page<>();
    //设置当前页
    page.setCurrent(curPage);
    //设置每页显示数据条数
    page.setSize(pageSize);
    return consumerService.allUser(page);
  }
  /**
   *删除用户
   */
  @GetMapping("/user/delete")
  public R deleteUser(@RequestParam int id) {
    return consumerService.deleteUser(id);
  }

  @DeleteMapping("user/deleteIds")
  public R deleteUsers(@RequestParam String[] id){
    return consumerService.deleteUsers(id);
  }
  //返回指定 ID 的用户
  @GetMapping("/user/detail")
  public R userOfId(@RequestParam int id) {
    return consumerService.userOfId(id);
  }

  @GetMapping("/users")
  public R getAllUser(){
    return consumerService.allUsers();
  }
  /**
   * 登录判断
   */
  @PostMapping("/user/login/status")
  public R loginStatus(@RequestBody ConsumerRequest loginRequest, HttpSession session) {
    R result = consumerService.loginStatus(loginRequest, session);
    Map<String,Object> map = new HashMap<>();
    map.put("res",result.getData());
    //登录成功的话，返回中需要包含token信息
    if(result.getSuccess()){
      String tokenStr = JwtUtils.generateToken(loginRequest.getUsername());
      Token token = new Token();
      token.setAuthorization(tokenStr);
      map.put("token",token);
    }
    result.setData(map);
    return result;
  }

  /**
   * TODO 前台页面调用 注册
   * 用户注册
   */
  @PostMapping("/user/add")
  public R addUser(@RequestBody ConsumerRequest registryRequest) {
    return consumerService.addUser(registryRequest);
  }

  /**
   * 前后台界面调用
   * 更新用户信息
   */
  @PostMapping("/user/update")
  public R updateUserMsg(@RequestBody ConsumerRequest updateRequest) {
    return consumerService.updateUserMsg(updateRequest);
  }

  /**
   * 更新用户头像
   */
  @PostMapping("/user/avatar/update")
  public R updateUserPic(@RequestParam("file") MultipartFile avatorFile, @RequestParam("id") int id) {
    return consumerService.updateUserAvator(avatorFile, id);
  }

  /**
   * 前后台更新用户密码
   */
  @PostMapping("/user/updatePassword")
  public R updatePassword(@RequestBody ConsumerRequest updatePasswordRequest) {
    return consumerService.updatePassword(updatePasswordRequest);
  }
}